<?php
namespace app\index\controller;

use think\Controller;

use think\Model; 

class Together extends Controller
{
    private function regexp($max,$begin,$qq,$tips)
    {
        $regexp_max='/^[0-9]{1,2}$/';
        $regexp_begin='/^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}\s[0-9]{1,2}:[0-9]{1,2}[:0-9]*$/';
        $regexp_qq='/^[0-9]{0,15}$/';
        $regexp_tips='/^[,，.。。a-zA-Z0-9\s\x{4e00}-\x{9fa5}]*$/u';
        if(preg_match($regexp_max,$max) && preg_match($regexp_begin,$begin) && preg_match($regexp_qq,$qq) && preg_match($regexp_tips,$tips))
            return true;
        else
            return false;   
    }

    public function issetCookie(){
        if (isset($_COOKIE["name"]))
            return true;
        else
            return false;
    }

    public function common()//拼场
    {   
        if(!$this->issetCookie())
            $this->error('当前功能需要登录才能使用','Index/login',5);
        else{
            $common=Model('common');
            $res=$common->where('dlt',0)->where('type',0)->order('begin asc')->select();
            $this->assign('common',$res);
            return $this->fetch();
        }
    }

    public function train()//教球
    {
        if(!$this->issetCookie())
            $this->error('当前功能需要登录才能使用','Index/login',5);
        $common=Model('common');
        $res=$common->where('dlt',0)->where('type',1)->order('begin asc')->select();
        $this->assign('teaching',$res);
        return $this->fetch();
    }

    public function publish()//发布信息
    {
        if(!$this->issetCookie())           
            $this->error('当前功能需要登录才能使用','Index/login',5);
    	return $this->fetch();
    }

    public function c_insert()//有人加入拼场更新数据库
    {
        $id=input('param.id');
        $regexp_number='/^[0-9]*$/';
        if(!preg_match($regexp_number,$id))
            $this->error('非法请求!','Index/index',3);
        if(!$this->issetCookie()){
            $this->redirect("Index/login",array("id"=>$id));
        }       
        $common=Model('common');
        $message=Model('message');
        $user=Model('user');
        $res=$common->where('id',$id)->find();
        if($res['max']==$res['now']){
            $this->error('人数已满','Index/index',3);
        }
        else if($res['fqr'] == cookie('name')){
            $this->error('该项由您本人发起','Index/index',3);
        }
        else if($res['cyr']==""){
            $msg=$user->where('username',cookie('xh'))->find();
            $people=$message->where('uid',$msg['id'])->find();
            $res['cyr']=$people['id'];
            $res['now']=$res['now']+1;
            $res->save();
            if($people['cy']==""){
                $people['cy']=$id;
            }
            else{
                $people['cy']=$people['cy']."+".$id;
            }
            $people->save();
            $this->success('加入成功','Index/index',3);
        }
        else{
            $msg=$user->where('username',cookie('xh'))->find();
            $people=$message->where('uid',$msg['id'])->find();
            if(strpos($res['cyr'],strval($people['id'])) === false){
                $res['cyr']=$res['cyr']."+".$people['id'];
                $res['now']=$res['now']+1;                
                if($people['cy']==""){
                    $people['cy']=$id;
                }
                else{
                    $people['cy']=$people['cy']."+".$id;
                }
                $res->save();
                $people->save();
                $this->success('加入成功','Index/index',3);
            } else {
                $this->success('不可重复加入','Index/index',3);
            }            
        }
    }

    public function p_insert()//插入发布的信息
    {
        $max=input('get.max');
        $year=input('get.b_year');
        $month=input('get.b_month');
        $day=input('get.b_day');
        $hour=input('get.b_hour');
        $minute=input('get.b_minute');
        $begin="";
        $begin=strval($year)."-".strval($month)."-".strval($day)." ".strval($hour).":".strval($minute);
        $qq=input('get.qq');
        $type=input('get.type');
        $time=input('get.time');
        $tips=input('get.tips');
        if($max=="" || $begin==""){
            $this->error('信息不完整请重新填写','Together/publish',5);
        }
        if(!$this->regexp($max,$begin,$qq,$tips)){
            $this->error('信息不合法请重新填写','Together/publish',15);
        }
        else if($type=="拼场"){
            $model=Model('common');
            $model->fqr=cookie('name');
            $model->cyr="";
            $model->dlt=0;
            $model->email=0;
            $model->memail=0;
            $model->max=$max;
            $model->now=1;
            $model->begin=$begin;
            $model->time=$time;
            $model->qq=$qq;
            $model->tips=$tips;
            $model->type=0;
            $model->save();
        }
        else if($type=="教球"){
            if(cookie('lv')==0)
                $this->error('权限不足无法发布教球信息','Together/publish',5);
            $model=Model('common');
            $model->fqr=cookie('name');
            $model->cyr="";
            $model->dlt=0;
            $model->email=0;
            $model->memail=0;
            $model->max=$max;
            $model->now=0;
            $model->begin=$begin;
            $model->time=$time;
            $model->qq=$qq;
            $model->tips=$tips;
            $model->type=1;
            $model->save();
        }
        $this->assign('name',cookie('name'));
        return $this->success('发布成功，5s后跳转到主页面','Index/index',5);
    }

    public function join()//我参与的拼场
    {
        if(!$this->issetCookie())
            $this->error('当前功能需要登录才能使用','Index/login',5);
        $message=Model('message');
        $res=$message->where('xm',cookie('name'))->find();
        $msg=array();
        $people=array();
        if($res['cy']!="")
        {           
            $cid=explode('+', $res['cy']);
            $common=Model('common');                  
            foreach ($cid as $value) {
                $join=$common->where('id',$value)->where('dlt',0)->where('type',0)->find();
                $id=explode("+", $join['cyr']);
                if($join['cyr'] != "")
                    foreach ($id as $value) {
                        $pmessage=$message->where('id',$value)->find();
                        $pmessage['cid']=$join['id'];                        
                        array_push($people, $pmessage);
                    }  
                $fqr=$message->where('xm',$join['fqr'])->find();
                if($join!=""){
                    $join['wx']=$fqr['wx'];
                    $join['fqq']=$fqr['qq'];
                    array_push($msg,$join);
                }
            }
        }
        $this->assign('people',$people);
        $this->assign('common',$msg);
    	return $this->fetch();
    }

    public function delete()//删除拼场
    {
        $id=input('get.id');
        $common=Model('common');
        $res=$common->where('id',$id)->find();
        if($res['cyr']==""){
            $res['dlt']=1;
            $res->save();
            $this->success('删除成功','Together/commonp',5);
        }
        else{
            $this->error('已经有人拼场无法删除','Together/commonp',5);
        }
    }

    public function cancel()//退出拼场
    {
        $id=input('get.id');
        $common=Model('common');
        $message=Model('message');
        $msg=$message->where('xm',cookie('name'))->find();
        $res=$common->where('id',$id)->find();
        $str=$res['cyr'];
        $res['cyr']=str_replace($msg['id'].'+', '', $res['cyr']);
        if($str==$res['cyr']){
            $res['cyr']=str_replace('+'.$msg['id'], '', $res['cyr']);
            if($res['cyr']==$str)
                $res['cyr']=str_replace($msg['id'], '', $res['cyr']);
        }
        $str=$msg['cy'];
        $msg['cy']=str_replace($id.'+', '', $msg['cy']);
        if($str==$msg['cy']){
           $msg['cy']=str_replace('+'.$id, '', $msg['cy']); 
           if($str==$msg['cy'])
            $msg['cy']=str_replace($id, '', $msg['cy']); 
        }
        $res['now']=$res['now']-1;
        $msg->save();
        $res->save();
        $this->success('成功退出','Index/index',5);
    }

    public function commonp()//我发布的拼场
    {
        if(!$this->issetCookie())
            $this->error('当前功能需要登录才能使用','Index/login',5);
        else{
            $common=Model('common');
            $message=Model('message');
            $res=$common->where('fqr',cookie('name'))->where('dlt',0)->select();
            $people=array();
            foreach ($res as  $val) {
                $id=explode("+", $val['cyr']);
                if($val['cyr'] != "")
                    foreach ($id as $value) {
                        $pmessage=$message->where('id',$value)->find();
                        $pmessage['cid']=$val['id'];
                        array_push($people, $pmessage);
                    }  
            }                    
            $this->assign('common',$res);
            $this->assign('people',$people);
            return $this->fetch();
        }
    }

    public function teaching()//我参加的训练
    {
        if(!$this->issetCookie())
            $this->error('当前功能需要登录才能使用','Index/login',5);
        $message=Model('message');
        $res=$message->where('xm',cookie('name'))->find();
        $msg=array();
        if($res['cy']!="")
        {
            $cid=explode('+', $res['cy']);
            $common=Model('common');       
            foreach ($cid as $value) {
                $train=$common->where('id',$value)->where('dlt',0)->where('type',1)->find();
                if($train!=""){
                    $teacher=$message->where('xm',$train['fqr'])->find();
                    $train['wx']=$teacher['wx'];
                    $train['fqq']=$teacher['qq'];
                    array_push($msg, $train);
                }
            }
        }
        $this->assign('train',$msg);
        return $this->fetch();
    }
}